//
//  MSStarEvaluator.m
//  Takeaway
//
//  Created by iOS on 2017/7/18.
//  Copyright © 2017年 MS. All rights reserved.
//

#import "MSStarEvaluator.h"
#import "MSMacro.h"

@interface MSStarEvaluator ()
{
    float           aWidth;         ///<一个星星+间隙的宽度
    float           aStarWidth;     ///< 一个星星的宽度
    float           aStarHeight;    ///< 一个星星的高度
    NSMutableArray  *fullStarArray;
}

@end

@implementation MSStarEvaluator

+ (MSStarEvaluator *)starEvaluatorWithValue:(CGFloat)initialValue
                                 dataSource:(id<MSStarEvaluatorDataSource>)dataSource
{
    MSStarEvaluator *view = [[self alloc] init];
    view.dataSource = dataSource;
    view.leave = initialValue;
    [view loadSubviews];
    
    return view;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.backgroundColor = UIColor.whiteColor;
        self.whole = NO;
    }
    return self;
}


- (void)loadSubviews
{
    fullStarArray = [NSMutableArray arrayWithCapacity:5];
    
    CGFloat space = [self.dataSource spaceForStarInStarEvaluator:self];
    CGSize size = [self.dataSource sizeForStarInStarEvaluator:self];
    aStarWidth = size.width;
    aStarHeight = size.height;
    aWidth = aStarWidth + space;
    
    NSString *emptyImgName = [self.dataSource emptyImageForStarInStarEvaluator:self];
    NSString *fullImgName = [self.dataSource fullImageForStarInStarEvaluator:self];
    
    for (int i = 0; i < 5; i ++)
    {
        UIImageView *emptyImgView = [[UIImageView alloc] initWithFrame:CGRectMake(i * aWidth,
                                                                                  0,
                                                                                  aStarWidth,
                                                                                  aStarHeight)];
        emptyImgView.backgroundColor = UIColor.clearColor;
        emptyImgView.image = [UIImage imageNamed:emptyImgName];
        [self addSubview:emptyImgView];
        
        UIImageView *fullImgView = [[UIImageView alloc] initWithFrame:CGRectMake(i * aWidth,
                                                                                 0,
                                                                                 aStarWidth,
                                                                                 aStarHeight)];
        fullImgView.backgroundColor = UIColor.clearColor;
        fullImgView.image = [UIImage imageNamed:fullImgName];
        
        CALayer *layer = [CALayer layer];
        layer.frame = CGRectMake(0, 0, 0, 0);
        layer.backgroundColor = [UIColor blackColor].CGColor;
        fullImgView.layer.mask = layer;
        
        [self addSubview:fullImgView];
        
        fullImgView.layer.mask.frame = CGRectMake(0, 0, 0, aStarHeight);
        
        [fullStarArray addObject:fullImgView];
    }
    
    [self draw];
}

- (void)setLeave:(CGFloat)leave
{
    _leave = leave;
    [self setNeedsDisplay];
}


#pragma mark - Tracking

- (BOOL)beginTrackingWithTouch:(UITouch *)touch
                     withEvent:(UIEvent *)event
{
    CGPoint touchPoint = [touch locationInView:self];
    
    int n = (int)(touchPoint.x / aWidth); // 整颗星数
    
    float f = (touchPoint.x - n * (aWidth - aStarWidth) - n * aStarWidth) / aStarWidth; // 半颗星比例
    if (_whole)
    {
        f = 1;
    }
    
    f = MIN(f, 1.0f);
    
    if (n + f < 0)
    {
        self.leave = 0;
    }
    else if (n + f > 5)
    {
        self.leave = 5;
    }
    else
    {
        self.leave = n + f;
    }
    
    return YES;
}

- (BOOL)continueTrackingWithTouch:(UITouch *)touch
                        withEvent:(UIEvent *)event
{
    CGPoint touchPoint = [touch locationInView:self];
    
    int n = (int)(touchPoint.x / aWidth); // 整颗星数
    float f = (touchPoint.x - n * (aWidth - aStarWidth) - n * aStarWidth) / aStarWidth; // 半颗星比例
    if (_whole)
    {
        f = 1;
    }
    
    f = MIN(f, 1.0f);
    
    if (n + f < 0)
    {
        self.leave = 0;
    }
    else if (n + f > 5)
    {
        self.leave = 5;
    }
    else
    {
        self.leave = n + f;
    }
    
    return YES;
}

- (void)draw
{
    int n = (int)_leave;
    float f = _leave - n;
    if (self.whole) f = 0;
    
    for (int i = 0; i < 5; i++)
    {
        UIImageView *fullImgView = fullStarArray[i];
        
        if (i < n)
        {
            // 整颗星
            fullImgView.layer.mask.frame = CGRectMake(0,
                                                      0,
                                                      aStarWidth,
                                                      aStarWidth);;
        }
        else if (i == n)
        {
            fullImgView.layer.mask.frame = CGRectMake(0,
                                                      0,
                                                      aStarWidth*f,
                                                      aStarWidth);
        }
        else
        {
            fullImgView.layer.mask.frame = CGRectZero;
        }
    }
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
    [CATransaction setDisableActions:!_animation];
    
    [self draw];
    
    if ([self.delegate respondsToSelector:@selector(starEvaluator:valueChanged:)])
    {
        [self.delegate starEvaluator:self
                        valueChanged:_leave];
    }
}


@end
